﻿#region MIT Licenze
//The MIT License

//Copyright (c) 2008 Sebastian Faltoni, dotnetfireball.org

//Permission is hereby granted, free of charge, to any person obtaining a copy
//of this software and associated documentation files (the "Software"), to deal
//in the Software without restriction, including without limitation the rights
//to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//copies of the Software, and to permit persons to whom the Software is
//furnished to do so, subject to the following conditions:

//The above copyright notice and this permission notice shall be included in
//all copies or substantial portions of the Software.

//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//THE SOFTWARE.
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Media;
using System.Xml.Linq;
using Fireball.Extensions;

namespace Fireball.OpenXML.Word
{
    /// <summary>
    /// Contains the information about the text border for 
    /// a paragraph
    /// </summary>
    public sealed class TextBorder : OpenXMLEntity
    {
        // a:color="auto"
        public Color? Color
        {
            get;
            set;
        }

        // a:frame="on"
        public bool? Frame
        {
            get;
            set;
        }

        // a:shadow="on"
        public bool? Shadow
        {
            get;
            set;
        }

        // a:space=""
        /// <summary>
        /// Border Space Measurement in Points
        /// </summary>
        public int? Space
        {
            get;
            set;
        }

        // a:sz="" 
        /// <summary>
        /// Border Size, Measurement in Eighths of a Point
        /// </summary>
        public decimal? Size
        {
            get;
            set;
        }

        public ThemeColor? ThemeColor
        {
            get;
            set;
        }

        // Two digit Hexadecimal Number value
        public int? ThemeShade
        {
            get;
            set;
        }

        // Two digit Hexadecimal Number value
        public int? ThemeTint
        {
            get;
            set;
        }

        // a:val="none"
        public BorderStyles Value
        {
            get;
            set;
        }


        public override System.Xml.Linq.XContainer ToXml()
        {
            XElement xBorder = new XElement(NS.WM + "bdr");

            if (this.Color != null)
                xBorder.Add(new XAttribute(NS.WM + "color", this.Color.Value.ToHexFormat()));
            else
                xBorder.Add(new XAttribute(NS.WM + "color", "auto"));

            if (this.Frame != null)
                xBorder.Add(new XAttribute(NS.WM + "frame", this.Frame.Value));

            if (this.Shadow != null)
                xBorder.Add(new XAttribute(NS.WM + "shadow", this.Shadow.Value));

            if (this.Space != null)
                xBorder.Add(new XAttribute(NS.WM + "space", this.Space.Value));

            if (this.Size != null)
                xBorder.Add(new XAttribute(NS.WM + "sz", this.Size.Value));

            if (this.ThemeColor != null)
                xBorder.Add(new XAttribute(NS.WM + "themeColor", this.ThemeColor.Value));

            if (this.ThemeShade != null)
                xBorder.Add(new XAttribute(NS.WM + "themeShade", this.ThemeShade.Value));

            if (this.ThemeTint != null)
                xBorder.Add(new XAttribute(NS.WM + "themeTint", this.ThemeTint.Value));

            xBorder.Add(new XAttribute(NS.WM + "val", this.Value.ToOffice()));//TODO: Serializzare il valore con la prima lettera in minuscolo


            return xBorder;
        }

        private void ParseInternal(XElement border)
        {
            var color = border.Attribute(NS.WM + "color");
            if (color != null)
            {
                if (color.Value.ToLower() == "auto")
                    this.Color = null;
                else
                    this.Color = (Color)ColorConverter.ConvertFromString(color.Value);
            }

            var frame = border.Attribute(NS.WM + "frame");
            if (frame != null)
            {
                this.Frame = true;
            }

            var shadow = border.Attribute(NS.WM + "shadow");
            if (shadow != null)
            {
                this.Shadow = true;
            }

            var space = border.Attribute(NS.WM + "space");
            if (space != null)
            {
                this.Space = Convert.ToInt32(space.Value);
            }

            var sz = border.Attribute(NS.WM + "sz");
            if (sz != null)
            {
                this.Size = Convert.ToDecimal(sz.Value);
            }

            var themeColor = border.Attribute(NS.WM + "themeColor");
            if (themeColor != null)
            {
                this.ThemeColor = (ThemeColor)Enum.Parse(typeof(ThemeColor),
                    themeColor.Value, true);
            }
            var themeShade = border.Attribute(NS.WM + "themeShade");
            if (themeShade != null)
            {
                this.ThemeShade = Convert.ToInt32(themeShade.Value);
            }
            var themeTint = border.Attribute(NS.WM + "themeTint");
            if (themeTint != null)
            {
                this.ThemeTint = Convert.ToInt32(themeTint.Value);
            }

            var val = border.Attribute(NS.WM + "val");
            if (val != null)
            {
                this.Value = (BorderStyles)Enum.Parse(typeof(BorderStyles),
                    val.Value, true);
            }
        }

        public static TextBorder Parse(XElement border)
        {
            TextBorder textBorder = new TextBorder();
            textBorder.ParseInternal(border);
            return textBorder;
        }
    }
}
